﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace IQIGame.Onigao.GameAOT
{
    public class HUDVertex
    {
        public Vector2 vecRU; // 右上角
        public Vector2 vecRD; // 右下角
        public Vector2 vecLD; // 左下角
        public Vector2 vecLU; // 左上角

        public Vector2 uvRD; // 右上角
        public Vector2 uvRU;
        public Vector2 uvLU;
        public Vector2 uvLD;

        public Color32 clrRD;
        public Color32 clrRU;
        public Color32 clrLU;
        public Color32 clrLD;

        public Vector3 WorldPos;
        // public Vector2 ScreenPos; // 屏幕坐标
        public Vector2 Offset; // 本地偏移
        public Vector2 Move; // 当前移动量(变动值)
        public float Scale;

        public Sprite NumSprite; // 图片ID
        public char ch; // 字
        public short width; // 图片或文字的宽度
        public short height; // 图片或文字的高度
        public HUDMesh hudMesh;
        public int m_hudVertexIndex; // 只能在HUDMesh中修改，切记

        public static Rect ConvertToTexCoords(Rect rect, int width, int height)
        {
            Rect final = rect;

            if (width != 0f && height != 0f)
            {
                final.xMin = rect.xMin / width;
                final.xMax = rect.xMax / width;
                final.yMin = 1f - rect.yMax / height;
                final.yMax = 1f - rect.yMin / height;
            }

            return final;
        }
        
        
        private static readonly bool m_UVStartsAtTop = SystemInfo.graphicsUVStartsAtTop;
        public static Vector4 GetOuterUV(Sprite sp)
        {
            var rect = sp.textureRect;
            var width = sp.texture.width;
            var height = sp.texture.height;

            float xMin = rect.xMin / width;
            float xMax = rect.xMax / width;
            float yMin;
            float yMax;

            if (m_UVStartsAtTop)
            {
                yMax = rect.yMax / height;
                yMin = rect.yMin / height;
            }
            else
            {
                yMin = rect.yMax / height;
                yMax = rect.yMin / height;
            }

            return new Vector4(xMin, yMin, xMax, yMax);
        }

        public void InitSprite(Color color,int nWidth = 0, int nHeight = 0)
        {
            ch = '\0';
            //AtlasID = -1;
            if (NumSprite == null)
                return;

            // modify
            //AtlasID = sp.m_nAtlasID;
            Rect texRect = NumSprite.textureRect;
            width = nWidth <= 0 ? (short) (texRect.width + 0.5f) : (short) nWidth;
            height = nHeight <= 0 ? (short) (texRect.height + 0.5f) : (short) nHeight;
            Scale = 1.0f;

            // modify
            // Rect mOuterUV = sp.outer;
            // UITexAtlas texAtlas = CAtlasMng.instance.GetAtlasByID(sp.m_nAtlasID);
            // if (texAtlas != null && texAtlas.coordinates == UITexAtlas.Coordinates.Pixels)
            // {
            //     mOuterUV = HUDVertex.ConvertToTexCoords(mOuterUV, texAtlas.texWidth, texAtlas.texHeight);
            // }

// #if UNITY_EDITOR
//             Vector4 mOuterUV = HUDVertex.GetOuterUV(NumSprite);
// #else
//             Vector4 mOuterUV = UnityEngine.Sprites.DataUtility.GetOuterUV(NumSprite);
// #endif
            Vector4 mOuterUV = UnityEngine.Sprites.DataUtility.GetOuterUV(NumSprite);


            float fL = 0.0f;
            float fT = 0.0f;
            float fR = texRect.width;
            float fB = texRect.height;
            //modify
            //float fR = width;
            //float fB = height;

            vecRU.Set(fR, fT); // 右上角
            vecRD.Set(fR, fB); // 右下角
            vecLD.Set(fL, fB); // 左下角
            vecLU.Set(fL, fT); // 左上角

            // modify
            // float uvR = mOuterUV.xMax;
            // float uvL = mOuterUV.xMin;
            // float uvB = mOuterUV.yMin;
            // float uvT = mOuterUV.yMax;

            // uvRU.Set(uvR, uvB);
            // uvRD.Set(uvR, uvT);
            // uvLD.Set(uvL, uvT);
            // uvLU.Set(uvL, uvB);

            uvRU.Set(mOuterUV.z, mOuterUV.y);
            uvRD.Set(mOuterUV.z, mOuterUV.w);
            uvLD.Set(mOuterUV.x, mOuterUV.w);
            uvLU.Set(mOuterUV.x, mOuterUV.y);

            clrLD = clrLU = clrRD = clrRU = color;
        }

        public void SlicedFill(int nWidth, int nHeight, float fOffsetX, float fOffsetY, float uvL, float uvT, float uvR,
            float uvB)
        {
            ch = '\0';
            Scale = 1.0f;

            float fL = fOffsetX;
            float fB = fOffsetY;
            float fR = fOffsetX + nWidth;
            float fT = fOffsetY + nHeight;

            vecRU.Set(fR, fB); // 右上角
            vecRD.Set(fR, fT); // 右下角
            vecLD.Set(fL, fT); // 左下角
            vecLU.Set(fL, fB); // 左上角

            uvRU.Set(uvR, uvT);
            uvRD.Set(uvR, uvB);
            uvLD.Set(uvL, uvB);
            uvLU.Set(uvL, uvT);

            clrLD = clrLU = clrRD = clrRU = Color.white;
            if (hudMesh != null)
                hudMesh.VertexDirty();
        }

        #region Pool

        HUDVertex m_pNext;
        int m_nID;
        static HUDVertex s_InvalidList;
        static int s_nInvalidCount = 0;
        static int s_nVertexID = 0;

        public static HUDVertex QueryVertex()
        {
            HUDVertex p = s_InvalidList;
            if (p != null)
            {
                s_InvalidList = p.m_pNext;
                --s_nInvalidCount;
                p.m_pNext = null;
            }

            if (p == null)
            {
                p = new HUDVertex();
                p.m_nID = ++s_nVertexID;
            }

            return p;
        }

        public static void ReleaseVertex(HUDVertex p)
        {
            if (p != null)
            {
                // p.hudGif = null;

                p.m_pNext = s_InvalidList;
                s_InvalidList = p;
                ++s_nInvalidCount;
            }
        }

        public int ID
        {
            get { return m_nID; }
        }

        #endregion
    }
}